package com.tentact.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tentact.mapper.PageUtil;
import com.tentact.service.DepartmentService;
import com.tentact.service.EmployeeService;
import com.tentact.service.RoleService;
import com.tentact.vo.Department;
import com.tentact.vo.Employee;
import com.tentact.vo.Role;


@Controller
public class EmployeeController {
	
	@Autowired
	private EmployeeService empSer;
	@Autowired
	private DepartmentService deptSer;
	@Autowired
	private RoleService roleSer;
	
	@RequestMapping("/")
	public String turnLogin() {
		return "login";
	}
	//登录方法,登录成功后跳转到最新通知页面notifications.jsp
	@RequestMapping("/login")
	public String login(String username,String userpwd,HttpServletRequest request,Model model) {
		Employee emp = empSer.selectByNameAndPwd(username, userpwd);
		if (emp != null) {
			//登陆成功  应该将用户信息存入到会话里  达到整个项目共享的效果
			HttpSession session=request.getSession();
			session.setAttribute("emp", emp);
			return "notifications";
		} else {
			//登陆失败
			String msg="用户名或密码错误/账户非正常状态";
			model.addAttribute("msg", msg);
			return "login";
		}
	}
	//获取所有部门和角色,返回json对象
	@RequestMapping("/selectDeptsAndRoles")
	@ResponseBody
	public Map<String, Object> selectDeptsAndRoles(){
		List<Department> depts=deptSer.selectAll();
		List<Role> roles=roleSer.selectAll();
		Map<String,Object> map=new HashMap<>();
		map.put("depts", depts);
		map.put("roles", roles);
		return map;
	}
	//注册用户
	@RequestMapping("/insertEmp")
	public String insertEmp(Employee employee) {
		empSer.insertSelective(employee);
		//跳转到待审批人员的一览界面  请求待审批人员的请求方法
		return "redirect:selectApproveAccount";
	}
	//查询待审核人员
	@RequestMapping("/selectApproveAccount")
	public String selectApproveAccount(Model model) {
		List<Employee> emps = empSer.selectByStatus(0);
		model.addAttribute("emps", emps);
		//跳转到待审批注册人员的一览页面  进行显示
		return "approveaccount";
	}
	//删除用户
	@RequestMapping("/delEmployee")
	public String delEmployee(Employee employee) {
		empSer.delete(employee);
		//跳回到待审核人员
		return "redirect:selectApproveAccount";
	}
	//审批用户
	@RequestMapping("/approveAccount")
	public String approveAccount(Employee employee) {
		empSer.updateSelective(employee);
		//跳转到待审批注册人员的一览页面  进行显示
		return "redirect:selectApproveAccount";
	}
	//关闭账户
	@RequestMapping("/closeAccount")
	public String closeAccount(Employee employee) {
		empSer.updateSelective(employee);
		//跳转到待审批注册人员的一览页面  进行显示
		return "redirect:searchEmployees?employeestatus=1";
	}
	//分页查询
	@RequestMapping("/searchEmployees")
	public String selectByPage(Employee employee,Integer currentpage,Model model) {
		if (currentpage == null) {
			currentpage = 1;
		}
		int recount = empSer.selectCountPage(employee);
		PageUtil page = new PageUtil(10, recount, currentpage);
		List<Employee> employees = empSer.selectByPage(employee, page);
		model.addAttribute("emps", employees);
		model.addAttribute("page",page);
		return "searchemployees";
	}
	
	
	//根据部门编号查询员工
	@RequestMapping("/selectByDeptno")
	@ResponseBody
	public List<Employee> selectByDeptno(Integer departmentid){
		List<Employee> employees =empSer.selectByDeptno(departmentid);
		return employees;
	}

}
